package com.edou.system.controller.admin;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.json.simple.JSONObject;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.edou.base.controller.BaseController;
import com.edou.base.token.TokenValidate;
import com.edou.base.util.NumberUtil;
import com.edou.system.entity.SysTag;
import com.edou.system.form.SysTagForm;
import com.edou.system.service.ISysTagService;



/**
 * 标签
 * @creator     
 * @create-time 2016/5/12 17:21:12
 * @email admin@edou.com
 * @company www.edou.com
 * @version 1.0
 */
@Controller("sysTagAdminController")
@Scope("prototype")
@RequestMapping("/admin/sys/tag")
public class SysTagAdminController extends BaseController {

	@Resource(name = "sysTagServiceImpl")
	private ISysTagService sysTagService;
	


	/**
	 * 首页
	 * @return 
	 */
	@RequestMapping(value = "/index.htm", method = RequestMethod.GET)
	public String index(){
		return "/admin/system/sys_tag_index";
	}

	/**
	 * 分页查询
	 * @return 
	 */
	@RequestMapping(value = "/listSysTagByPage.htm", method = RequestMethod.POST)
	public void listSysTagByPage(@ModelAttribute SysTagForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		try{
			sysTagService.listSysTagByPage(model.getPageResult(),model.getCondition());
			JSONObject root = toPageJson(model.getPageResult(), new String[]{"id","name","type","desc","sort"});
			ajaxPageResult(root,response);
		}catch(Exception ex){
			String msg = "查询标签时发生异常："+ex.getMessage();
			logger.error(msg,ex);
		}
	}

	/**
	 * 保存标签
	 * @return 
	 */
	@RequestMapping(value = "/edit.htm", method = RequestMethod.GET)
	public ModelAndView edit(@ModelAttribute SysTagForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		ModelAndView view = new ModelAndView();
		view.setViewName("/admin/system/sys_tag_add");
		Map<String,Object> data = view.getModel();
		if(null != model.getItem().getId()){
		SysTag item = sysTagService.getSysTagById(model.getItem().getId());
			data.put("item", item);
		}
		


		return view;
	}

	/**
	 * 保存标签
	 * @return 
	 */
	@RequestMapping(value = "/saveSysTag.htm", method = RequestMethod.POST)
	@TokenValidate
	public ModelAndView saveSysTag(@ModelAttribute SysTagForm model,HttpServletRequest request,HttpSession session){
		try {
			if(null == model.getItem().getId()){
				sysTagService.add(model.getItem());
			}else{
				sysTagService.update(model.getItem());
			}
			return ajaxJSON(Status.success,"保存成功");
		} catch (Exception e) {
			String msg = "保存标签时发生异常：" + e.getMessage();
			logger.error(msg,e);
			return ajaxJSON(Status.error, "保存失败，请稍后重试");
		}
	}
	/**
	 * 删除标签
	 * @return 
	 */
	@RequestMapping(value = "/del.htm", method = RequestMethod.POST)
	public ModelAndView del(@ModelAttribute SysTagForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		try {
			sysTagService.delByIds(NumberUtil.convertStringArrayToLongArray(model.getSelIds().split(",")));
			return ajaxJSON(Status.success,"删除成功");
		} catch (Exception e) {
			String msg = "删除标签时发生异常：" + e.getMessage();
			logger.error(msg,e);
			return ajaxJSON(Status.error, "删除失败，请稍后重试");
		}
	}
	
	/**
	 * 修改标签类型
	 * @return 
	 */
	@RequestMapping(value = "/update.htm", method = RequestMethod.POST)
	public ModelAndView update(@ModelAttribute SysTagForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		try {
			
			String ids = model.getSelIds();
			Integer type = model.getItem().getType();
			
			if(type != null && StringUtils.isNotBlank(ids) && type == 1){
				Long [] tagIds = NumberUtil.convertStringArrayToLongArray(ids.split(","));
				for (Long id : tagIds) {
					SysTag tag = sysTagService.getSysTagById(id);
					tag.setType(type);
					sysTagService.update(tag);
				}
			}else if(type != null && StringUtils.isNotBlank(ids) && type == 0){
				Long [] tagIds = NumberUtil.convertStringArrayToLongArray(ids.split(","));
				for (Long id : tagIds) {
					SysTag tag = sysTagService.getSysTagById(id);
					tag.setType(type);
					sysTagService.update(tag);
				}
			}
			return ajaxJSON(Status.success,"设置成功");
			
		} catch (Exception e) {
			String msg = "设置热门标签时发生异常：" + e.getMessage();
			logger.error(msg,e);
			return ajaxJSON(Status.error, "设置热门标签失败，请稍后重试");
		}
	}

}